<?php
namespace App\Services;

use Illuminate\Http\Client\RequestException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Redis;
class DaHuangAdminService {
    private static $errorMessage = [
        -1    => '系统错误,请稍后再试',
        1001  => '参数不能为空',
        1002  => '参数不能验证通过',
        1003  => '参数错误',
        2001  => 'Token不能为空',
        2002  => 'Token非法',
        2003  => 'Token已过期',
        2004  => 'Api禁止访问',
        2005  => 'Api超过访问上限次数',
        2006  => '应用已被禁止访问',
        2007  => 'AppId或者AppSecret错误',
        3001  => '机器不存在',
        3002  => '机器不属于该机构'
    ];
    /**
     * @notes 获取请求token
     * @param $appId
     * @param $appSecret
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/06/06 15:33
     */
    private static function getRequestToken($appId, $appSecret)
    {
        $token = Redis::get('dhe:request_admin_token');

        if($token) return $token;

        $path = '/token/info';

        $params = [
            'appId'     => $appId,
            'appSecret' => $appSecret
        ];

        $res = self::commonRequest($path, $params);

        $token = $res['accessToken'];

        Redis::setex('dhe:request_admin_token', 60*60*2, $token);

        return $token;
    }

    /**
     * @notes 发起公共请求
     * @param $path
     * @param $data
     * @param $header
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/06/06 15:33
     */
    private static function commonRequest($path, $data, $header = [])
    {
        $host = config('services.dhe.adminHost');

        $url = $host . $path;

        $response = Http::retry(3, 1000)
            -> timeout(10)
            -> withHeaders($header)
            -> get($url, $data)
            -> throw()
            -> json();

        if(!$response) {
            throw new AccessDeniedHttpException('DHE服务请求失败');
        }

        if($response['code'] != 100) {
            if(key_exists($response['code'], self::$errorMessage)) {
                throw new AccessDeniedHttpException(self::$errorMessage[$response['code']]);
            } else {
                throw new AccessDeniedHttpException($response['message']);
            }
        }

        return $response['content'];
    }

    /**
     * @notes Api文档接口请求
     * @param $path
     * @param $params
     * @return mixed
     * @throws \Illuminate\Http\Client\RequestException
     * @author Evil丿
     * @date 2024/03/28 11:23
     */
    private static function apiRequest($path, $params = [])
    {
        $appId = config('services.dhe.appId');

        $appSecret = config('services.dhe.appSecret');

        $header = [
            'accessToken' => self::getRequestToken($appId, $appSecret)
        ];

        return self::commonRequest($path, $params, $header);
    }

    /**
     * @notes 获取设备列表
     * @param $machine_code
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/06/19 17:20
     */
    public static function getMachineList($machine_code = "")
    {
        $params = $machine_code ? ['machine_code' => $machine_code] : [];

        $path = '/machine/list';

        $res = self::apiRequest($path, $params);

        return $res;
    }

    /**
     * @notes 获取设备详情
     * @param $machine_code
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/06/20 10:54
     */
    public static function getMachineInfo($machine_code)
    {
        $params = [
            'machineCode' => $machine_code
        ];

        $path = '/machine/info';

        $res = self::apiRequest($path, $params);

        return $res;
    }

    /**
     * @notes 获取大黄鹅商品列表
     * @param $start_time
     * @param $end_time
     * @return mixed
     * @throws RequestException
     * @author Evil丿
     * @date 2024/06/21 17:03
     */
    public static function getGoodsList($start_time = "", $end_time = "")
    {
        $params = [
            'beginTime' => $start_time,
            'endTime'   => $end_time,
        ];

        $path = '/goods/list';

        $res = self::apiRequest($path, $params);

        return $res;
    }

    public static function getMachineGoods($machine_code)
    {
        $params = [
            'machineCode' => $machine_code
        ];

        $path = '/machine/ailse/info';

        $res = self::apiRequest($path, $params);

        return $res;
    }
}
